<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCateShowDialogBtnFn"
          >添加分类</el-button
        >
      </div>
      <el-table :data="cateList" style="width: 100%" border stripe>
        <el-table-column
          type="index"
          label="序号"
          width="100"
        ></el-table-column>
        <el-table-column prop="name" label="分类名称"></el-table-column>
        <el-table-column prop="alias" label="分类别名"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              @click="updateCateBtnFn(scope.row)"
              type="primary"
              size="mini"
              >修改</el-button
            >
            <el-button
              @click="deleteCateBtnFn(scope.row.id)"
              type="danger"
              size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      @close="onAddDialogClosed"
      :title="isEdit ? '修改文章分类' : '新增文章分类'"
      :visible.sync="dialogVisible"
      width="35%"
    >
      <el-form
        :model="addForm"
        ref="addRef"
        :rules="addRuls"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="alias">
          <el-input v-model="addForm.alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getArtCateListAPI,
  saveArtCateAPI,
  updateArtCateAPI,
  delArtCateAPI
} from '@/api'
export default {
  name: 'my-artCate',

  data () {
    return {
      isEdit: false, // true为编辑状态，false为新增状态
      editId: '', // 保存正在编辑的id
      cateList: [],
      dialogVisible: false,
      addForm: { name: '', alias: '' },
      addRuls: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted () {
    this.getCateListFn()
  },

  methods: {
    async getCateListFn () {
      const { data: res } = await getArtCateListAPI()
      this.cateList = res.data
    },
    addCateShowDialogBtnFn () {
      this.isEdit = false
      this.editId = ''
      this.dialogVisible = true
    },
    onAddDialogClosed () {
      // 清空表单
      this.$refs.addRef.resetFields()
    },
    cancelFn () {
      this.dialogVisible = false
    },
    confirmFn () {
      this.$refs.addRef.validate(async (valid) => {
        if (valid) {
          if (this.isEdit) {
            const { data: res } = await updateArtCateAPI({
              id: this.editId,
              ...this.addForm
            })
            if (res.status !== 0) return this.$message.error(res.message)
            this.$message.success(res.message)
          } else {
            const { data: res } = await saveArtCateAPI(this.addForm)
            if (res.status !== 0) return this.$message.error(res.message)
            this.$message.success(res.message)
          }
          this.getCateListFn()
          this.dialogVisible = false
        } else {
          return false
        }
      })
      this.dialogVisible = false
    },
    updateCateBtnFn (obj) {
      this.dialogVisible = true
      this.isEdit = true

      // 先渲染弹窗，然后赋值，不然出现bug
      this.$nextTick(() => {
        this.editId = obj.id
        this.addForm.name = obj.name
        this.addForm.alias = obj.alias
      })
    },
    deleteCateBtnFn (id) {
      this.$confirm('要确实永久删除吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteCateFn(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作已取消'
        })
      })
    },
    async deleteCateFn (id) {
      const { data: res } = await delArtCateAPI(id)
      console.log(res)
      if (res.status !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getCateListFn()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
